package com.skcc.cop.mrtaxi.http;

import java.net.SocketTimeoutException;

import android.content.Context;

import com.projectteam.http.api.BaseHttpApi;
import com.projectteam.http.exception.NoNetworkException;
import com.projectteam.http.exception.NoResponseException;
import com.projectteam.http.util.DeviceUtil;
import com.skcc.cop.mrtaxi.biz.IntroActivity;
import com.skcc.cop.mrtaxi.http.json.GetCallHistListReq;
import com.skcc.cop.mrtaxi.http.json.GetCallHistListRes;
import com.skcc.cop.mrtaxi.http.json.CallReq;
import com.skcc.cop.mrtaxi.http.json.CallRes;
import com.skcc.cop.mrtaxi.http.json.CommonHeader;
import com.skcc.cop.mrtaxi.http.json.GetDriverListReq;
import com.skcc.cop.mrtaxi.http.json.GetDriverListRes;
import com.skcc.cop.mrtaxi.http.json.LoginReq;
import com.skcc.cop.mrtaxi.http.json.LoginRes;
import com.skcc.cop.mrtaxi.http.json.RegisterDestReq;
import com.skcc.cop.mrtaxi.http.json.RegisterDestRes;
import com.skcc.cop.mrtaxi.http.json.RegisterPushIdReq;
import com.skcc.cop.mrtaxi.http.json.RegisterPushIdRes;
import com.skcc.cop.mrtaxi.http.model.Destination;
import com.skcc.cop.mrtaxi.http.model.Result;
import com.skcc.cop.mrtaxi.util.Utils;

public class BizGateway extends BaseHttpApi {
	private final static String TAG = "BizGateway";
	
	private Context context;
	
	
	public BizGateway(Context context, String requestUrl) {
		super(context, requestUrl);
		
		this.context = context;
	}
	

	private CommonHeader getCommonHeader() {
	    	
    	DeviceUtil device = new DeviceUtil(context);
    	
    	CommonHeader commonHeader = new CommonHeader();
    	commonHeader.setTransactionId(device.getMDN() + "_" + Utils.getFormattedDate());
    	commonHeader.setImei(device.getIMEI());
    	commonHeader.setMsisdn(device.getMDN());
    	commonHeader.setAppVersion(device.getAppVersion());
    	commonHeader.setOsModel(device.getDeviceModel());
    	commonHeader.setOsVersion(device.getOsVersion());
    	
    	return commonHeader;
    }
	
	
	/**
	 * registration id 을 서버로 전송하는 메소드
	 * @param pushId 단말의 registrationId
	 * @param listener
	 */
	public void registerPushId(final String pushId, final RegisterPushIdListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				RegisterPushIdReq req = new RegisterPushIdReq();
				req.setCommonHeader(getCommonHeader());
				req.setPushId(pushId);
				final RegisterPushIdRes res;
				
				try {
					res = (RegisterPushIdRes) getResponse(req, RegisterPushIdRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess();
						}
						
					}
				});
			}
		}).start();
		
	}
	
	
	/**
	 * 로그인 전문
	 * @param email
	 * @param password
	 * @param listener
	 */
	public void login(final String email, final String password, final LoginListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				LoginReq req = new LoginReq();
				req.setCommonHeader(getCommonHeader());
				req.setEmail(email);
				req.setPassword(password);
				final LoginRes res;
				
				try {
					res = (LoginRes) getResponse(req, LoginRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess();
						}
						
					}
				});
			}
		}).start();
	}
	
	
	/**
	 * Call History 내역 조회
	 * @param deviceId
	 * @param listener
	 */
	public void getCallHistoryList(final String deviceId, final CallHistoryListListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				GetCallHistListReq req = new GetCallHistListReq();
				req.setCommonHeader(getCommonHeader());
				req.setDriverId(deviceId);
				final GetCallHistListRes res;
				
				try {
					res = (GetCallHistListRes) getResponse(req, GetCallHistListRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess(res.getCallHistList());
						}
						
					}
				});
			}
		}).start();
		
	}
	

	/**
	 * Sample - 내 주변의 기사 조회
	 * @param req
	 * @return
	 * @throws NoNetworkException
	 */
	public void getTaxiDriverList(final double latitude, final double longitude, final GetDriverListListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				GetDriverListReq req = new GetDriverListReq();
				req.setCommonHeader(getCommonHeader());
				req.setLatitude(Double.valueOf(latitude));
				req.setLongitude(Double.valueOf(longitude));
				final GetDriverListRes res;
				
				try {
					res = (GetDriverListRes) getResponse(req, GetDriverListRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess(res.getDriverList());
						}
						
					}
				});
			}
		}).start();
		
	}
	
	/**
	 * 목적지 등록/수정
	 * @param Destination
	 * @param listener
	 */
	public void RegisterDestination(final Destination dest,  final DestinationListener listener) {
		new Thread(new Runnable() {
			public void run() {

				RegisterDestReq req = new RegisterDestReq();
				req.setCommonHeader(getCommonHeader());
				req.setPsgLatitude(dest.getPsgLatitude());
				req.setPsgLongitude(dest.getPsgLongitude());
				req.setPsgAddr1(dest.getPsgAddr1());
//				req.setPsgAddr2(dest.getPsgAddr2());
//				req.setPsgAddr3(dest.getPsgAddr3());
//				req.setPsgAddr4(dest.getPsgAddr4());
				req.setPsgNum(dest.getPsgNum());
				req.setUserId(dest.getUserId());
				req.setDestRegtEdit(dest.getDestRegtEdit());

				final RegisterDestRes res;

				try {
					res = (RegisterDestRes) getResponse(req, RegisterDestRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}

				Utils.makeLooperThread(new Runnable() {
					public void run(){

						if (res == null) {
							listener.onNoResponse();
							return;
						}

						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess();
						}

					}
				});
			}
		}).start();

	}
	
	public void call(final String driverId, final String userId, final String psgAddr1, final int psgNum, final double userLatitude, final double userLongitude, final CallListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				CallReq req = new CallReq();
				req.setCommonHeader(getCommonHeader());
				req.setDriverId(driverId);
				req.setUserId(userId);
				req.setPsgAddr1(psgAddr1);
				req.setPsgNum(psgNum);
				req.setUserLatitude(userLatitude);
				req.setUserLongitude(userLongitude);
				final CallRes res;
				
				try {
					res = (CallRes) getResponse(req, CallRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess(res.getAcceptYn());
						}
						
					}
				});
			}
		}).start();
		
	}
	
}
